﻿using System.Collections.Generic;
using IDAL;
using Model;
using NHibernate;

namespace SqlDALLibrary
{
    public class KeywordDal : IKeywordDAL
    {
        private static EntityControl control;

        static KeywordDal()
        {
            control = EntityControl.CreateEntityControl("Model");
        }

        public void saveKeyword(Keyword keyword)
        {
            NHibernateHelper nh = new NHibernateHelper();
            ISession session = nh.GetSession();
            using (var tx = session.BeginTransaction())
            {
                session.Save(keyword);
                tx.Commit();
            }
            session.Close();
        }

        public void saveKeyword(IList<Keyword> keywordList)
        {
            NHibernateHelper nh = new NHibernateHelper();
            ISession session = nh.GetSession();
            using (var tx = session.BeginTransaction())
            {
                foreach (Keyword keyword in keywordList)
                {
                    session.Save(keyword);
                }
                tx.Commit();
            }
            session.Close();
        }

        public void updateKeyword(Keyword keyword)
        {
            NHibernateHelper nh = new NHibernateHelper();
            ISession session = nh.GetSession();
            using (var tx = session.BeginTransaction())
            {
                session.Update(keyword);
                tx.Commit();
            }
            session.Close();
        }

        public void delkeyword(Keyword keyword)
        {
            NHibernateHelper nh = new NHibernateHelper();
            ISession session = nh.GetSession();
            using (var tx = session.BeginTransaction())
            {
                session.Delete(keyword);
                tx.Commit();
            }
            session.Close();
        }

        public void delkeyword(Resource resource)
        {
            NHibernateHelper nh = new NHibernateHelper();
            ISession session = nh.GetSession();
            session.CreateQuery("delete from Keyword k where k.resource=:resource")
                                .SetParameter("resource", resource);
            session.Close();
        }

        public IList<Keyword> getKeywordList()
        {
            return control.getSession()
                .CreateQuery("from Keyword order by resourceListIndex")
                .List<Keyword>();
        }

        public Keyword getKeyword(int id)
        {
            NHibernateHelper nh = new NHibernateHelper();
            ISession session = nh.GetSession();
            Keyword keyword = session
                   .CreateQuery("from Keyword k where k.id=:id")
                .SetInt32("id", id)
                .UniqueResult<Keyword>();
            session.Close();
            return keyword;
        }

        public Keyword getKeyword(string name)
        {
            NHibernateHelper nh = new NHibernateHelper();
            ISession session = nh.GetSession();
            Keyword keyword = session
                   .CreateQuery("from Keyword k where k.keywordName=:keywordName")
                .SetString("keywordName", name)
                .UniqueResult<Keyword>();
            session.Close();
            return keyword;
        }

        public IList<Keyword> getResourceListByKeyword(string keywordName)
        {
            NHibernateHelper nh = new NHibernateHelper();
            ISession session = nh.GetSession();
            IList<Keyword> resourceList = session
                .CreateQuery("from Keyword r where r.keywordName=:keywordName")
                .SetString("keywordName", keywordName)
                .List<Keyword>();
            session.Close();
            return resourceList;
        }

        public IList<Keyword> getKeywordList(Resource resource)
        {
            NHibernateHelper nh = new NHibernateHelper();
            ISession session = nh.GetSession();
            IList<Keyword> keywordList = session
                    .CreateQuery("from Keyword k where k.resource=:resource")
                    .SetParameter("resource", resource)
                    .List<Keyword>();
            session.Close();
            return keywordList;
        }

    }
}
